<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="所属院系">
              <a-select :disabled="selectMenu.yx.length === 1" allowClear v-model="queryParam.pydw" @change="yxChange" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="教师">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="12">
              <a-form-item label="开课学年">
                <a-select allowClear v-model="queryParam.kkxn">
                  <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="开课学期">
                <a-select allowClear v-model="queryParam.kkxq">
                  <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!-- <a-col :md="8" :sm="12">
              <a-form-item label="课程类别">
                <a-select allowClear v-model="queryParam.kclb" @change="lbChange">
                  <a-select-option v-for="i in TYPE0359" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="12">
              <a-form-item label="课程名称">
                <a-select allowClear v-model="queryParam.kcbh" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.kc" :key="i.id" :value="i.kch">{{ i.kcmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="开课年级">
                <a-select allowClear v-model="queryParam.kknj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="上课周类型">
                <a-select allowClear v-model="queryParam.skzlxm">
                  <a-select-option v-for="i in TYPE0343" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="上课周">
                <a-select allowClear v-model="queryParam.skz">
                  <a-select-option v-for="i in TYPE0344" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="开始周">
                <a-input @change="onNumChange" v-model="queryParam.ksz"/>
              </a-form-item>
            </a-col>
             <a-col :md="8" :sm="12">
              <a-form-item label="结束周">
                <a-input @change="onNumChange" v-model="queryParam.jsz"/>
              </a-form-item>
            </a-col>
             <a-col :md="8" :sm="12">
              <a-form-item label="开始课节">
                <a-input @change="onNumChange" v-model="queryParam.kskj"/>
              </a-form-item>
            </a-col>
             <a-col :md="8" :sm="12">
              <a-form-item label="结束课节">
                <a-input @change="onNumChange" v-model="queryParam.jskj"/>
              </a-form-item>
            </a-col> -->
          </template>
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="exportExcel" style="margin:0 10px">导出</a-button>
            <a-button @click="queryParam = {};$refs.table.refresh()">重置</a-button>

            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="{show: true}"
        :pagination="{ pageSize: 10 }"
      >
        <div
          slot="expandedRowRender"
          slot-scope="record"
          style="margin: 0">
          <a-row
            v-for="(pkxx, index) in record.pkxxs" :key="index"
            :gutter="8"
            style="word-wrap: break-word;margin-bottom: 2px">
              <a-col :span="2">
                <span>{{ pkxx.kcbm }}</span>
              </a-col>
              <a-col :span="2">
                <span>{{ pkxx.kclbmc }}</span>
              </a-col>
              <a-col :span="3">
                <span>{{ pkxx.kcmc }}</span>
              </a-col>
              <a-col :span="2">
                <span>{{ pkxx.skzlxmc }}</span>
              </a-col>
              <a-col :span="2">
                <span>开始周：{{ pkxx.ksz }}</span>
              </a-col>
              <a-col :span="2">
                <span>结束周：{{ pkxx.jsz }}</span>
              </a-col>
              <a-col :span="2">
                <span>上课周：{{ pkxx.skzmc }}</span>
              </a-col>
              <a-col :span="2">
                <span>开始课节：{{ pkxx.kskj }}</span>
              </a-col>
              <a-col :span="2">
                <span>结束课节：{{ pkxx.jskj }}</span>
              </a-col>
              <a-col :span="3">
                <span>上课地点：{{ pkxx.location }}</span>
              </a-col>
          </a-row>
        </div>
      </s-table>
    </a-card>

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {
        kkxn:'',
        kkxq: ''
      },
      columns: [
        {
          title: '工号',
          dataIndex: 'gh'
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '所属院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '教师职称',
          dataIndex: 'zcmc'
        }
      ],
      loadData: parameter => {
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.teacherPkList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {
        yx:[]
      },
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      initDone: false
    }
  },
  components: {STable},
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0125: state => state.app['TYPE0125'],
      TYPE0359: state => state.app['TYPE0359'],
      TYPE0126: state => state.app['TYPE0126'],
      TYPE0343: state => state.app['TYPE0343'],
      TYPE0344: state => state.app['TYPE0344'],
      TYPE0072: state => state.app['TYPE0072']
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    this.$store.dispatch('app/setDictionary', 'TYPE0343')
    this.$store.dispatch('app/setDictionary', 'TYPE0344')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0359')
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.initSelectMenu()
    // this.$store.dispatch('app/sysInfo').then(res => {
    //   this.queryParam.kkxn = this.systemInfo.schoolYear
    //   this.queryParam.kkxq = this.systemInfo.currentSemester
    //   this.$refs.table.refresh()
    // })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.$set(this.queryParam, 'kkxq', res[1].currentSemester)
        this.$set(this.queryParam, 'kkxn', res[1].schoolYear)
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'pydw', this.selectMenu.yx[0].dwh)
          this.yxChange(this.queryParam.ssyxm)
        }
        this.initDone = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'kcbh', '')
      if (code) {
        this.$api.cultivate.courseList({kcssyxm: code, kclbm: this.queryParam.kclb}).then(res => {
          this.$set(this.selectMenu, 'kc', res.data.rows)
        })
      } else {
        this.$set(this.selectMenu, 'kc', [])
      }
    },
    lbChange (code) {
      this.$set(this.queryParam, 'kcbh', '')
      if (code) {
        this.$api.cultivate.courseList({kcssyxm: this.queryParam.pydw, kclbm: code}).then(res => {
          this.$set(this.selectMenu, 'kc', res.data.rows)
        })
      } else {
        this.$set(this.selectMenu, 'kc', [])
      }
    },
    onNumChange (e){
      const { value } = e.target
      const reg = /^-?(0|[1-9][0-9]*)(\.[0-9]*)?$/
      if ((!isNaN(value) && reg.test(value)) || value === '' || value === '-') {
        this.value = value
      }
    },
    exportExcel () {
      let params = {...this.queryParam}
      delete params.pageNum
      this.$export('/foster/pkxx/teacherPkqkExp', params)
    }
  }
}
</script>

<style>

</style>
